package com.justnow.offer;

import jdk.nashorn.internal.ir.CallNode;

/**
 * @author justnow
 * Created on 2020-09-14
 * Description
 */
public class Shunfeng02 {

    static int[] findNumber(int num) {
        int[] res = {-1, -1};
        long max = (long)num << 1;
        int min = num >> 1;
        int oneNumber = getNumberOfOne(num);
        for (int i = num + 1; i <= max; i++) {
            if (oneNumber == getNumberOfOne(i)) {
                res[0] = i;
                break;
            }
        }
        for (int i = num - 1; i >= min; i--) {
            if (oneNumber == getNumberOfOne(i)) {
                res[1] = i;
                break;
            }
        }
        return res;
    }

    static int getNumberOfOne(long num) {
        String str = Long.toBinaryString(num);
        int res = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '1') {
                res++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int num = 15;
        while (num > 0) {
            System.out.println(Integer.toBinaryString(num));
            num = num >> 1;
        }
    }
}
